class TournamentsController < ApplicationController
  before_filter :require_user, :except => [:index]
  before_filter :is_join_correct, :only => [:join]
  
  # GET /tournaments
  # GET /tournaments.xml
  def index
    condition = "start_date < '" + Date.today.to_s + "' " if(params[:query] == "archives")
    condition = "start_date >= '" + Date.today.to_s + "' " if(not params[:query])
    
    @tournaments = Tournament.all(:order => "start_date", :conditions => [condition])
    @current_user_tournaments = current_user.tournaments  if current_user
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tournaments }
    end
  end
  
  def join
    @user_tournament = UserTournament.new(:user_id => current_user.id, :tournament_id => params[:id])
    
    if @user_tournament.save
      flash[:notice] = 'UserTournament linking was successfully created.'
    else
      flash[:notice] = 'UserTournament linking echec...'
    end
    
    respond_to do |format|
      format.html { redirect_to(tournaments_url) }
    end
  end
  
  def unjoin
    begin
      @user_tournaments = UserTournament.find(:all,:conditions => {:user_id => current_user, :tournament_id => params[:id]})
      @user_tournaments.each do |user_tournament|
        user_tournament.destroy
      flash[:notice] = 'UserTournament link destroyed.'
      end
    rescue ActiveRecord::RecordNotFound
      flash[:notice] = 'UserTournament link does\'nt exist.'
    end
    respond_to do |format|
      format.html { redirect_to(tournaments_url) }
    end
  end
  
  # GET /tournaments/new
  # GET /tournaments/new.xml
  def new
    @tournament = Tournament.new
    
    respond_to do |format|
      format.html # new.html.erb@tournament
      format.xml  { render :xml => @tournament }
    end
  end
  
  # GET /tournaments/1/edit
  def edit
    @tournament = Tournament.find(params[:id])
  end
  
  # POST /tournaments
  # POST /tournaments.xml
  def create
    @tournament = Tournament.new(params[:tournament])
    
    respond_to do |format|
      if @tournament.save
        flash[:notice] = 'Tournament was successfully created.'
        format.html { redirect_to(tournaments_url) }
        format.xml  { render :xml => @tournament, :status => :created, :location => @tournament }
      else
        flash[:notice] = 'Tournament was not successfully created...'
        format.html { render :action => "new" }
        format.xml  { render :xml => @tournament.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /tournaments/1
  # PUT /tournaments/1.xml
  def update
    @tournament = Tournament.find(params[:id])
    
    respond_to do |format|
      if @tournament.update_attributes(params[:tournament])
        flash[:notice] = 'Tournament was successfully updated.'
        format.html { redirect_to(tournaments_url) }
        format.xml  { head :ok }
      else
        flash[:notice] = 'Tournament was not successfully updated...'
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tournament.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /tournaments/1
  # DELETE /tournaments/1.xml
  def destroy
    @tournament = Tournament.find(params[:id])
    @tournament.destroy
    
    respond_to do |format|
      format.html { redirect_to(tournaments_url) }
      format.xml  { head :ok }
    end
  end
end
